package com.zx.email.controller;

import Email.Dto.EmailDto;
import com.zx.email.service.EmailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Result;

import java.util.List;

@Tag(name = "邮件相关接口", description = "提供邮件相关操作的接口")
@RestController
@RequestMapping("/emails")
@RequiredArgsConstructor


public class EmailController {

    @Autowired
    EmailService emailService;

    // 通过发送方id查询所有邮件
    @Operation(summary = "根据发送方公司ID查询所有邮件",
            description = "根据传入的发送方公司ID，查询对应的所有邮件信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功",
                            content = @Content(mediaType = "application/json",
                                    schema = @Schema(implementation = Result.class)))
            })
    @GetMapping("/byCompanyId")
    public Result<List<EmailDto>> findALlByCompanyId(
            @Parameter(in = ParameterIn.QUERY, description = "发送方公司ID", required = true, schema = @Schema(type = "integer"))
            @RequestParam Integer companyId) {
        return Result.success(emailService.findALlByCompanyId(companyId));
    }

    //
    // 通过接收方id查询所有邮件
    @Operation(summary = "根据接收方用户ID查询所有邮件",
            description = "根据传入的接收方用户ID，查询对应的所有邮件信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功",
                            content = @Content(mediaType = "application/json",
                                    schema = @Schema(implementation = Result.class)))
            })
    @GetMapping("/byUserId")
    public Result<List<EmailDto>> findALlByUserId(
            @Parameter(in = ParameterIn.QUERY, description = "接收方用户ID", required = true, schema = @Schema(type = "integer"))
            @RequestParam Integer userId) {
        return Result.success(emailService.findALlByUserId(userId));
    }

    //
    // 发送邮件
    @Operation(summary = "发送邮件",
            description = "根据传入的邮件数据传输对象（EmailDto），发送邮件",
            responses = {
                    @ApiResponse(responseCode = "200", description = "邮件发送成功",
                            content = @Content(mediaType = "application/json",
                                    schema = @Schema(implementation = Result.class)))
            })
    @PostMapping("/send")
    public Result<?> sendEmail(
//            @Parameter(in = ParameterIn.BODY, description = "邮件数据传输对象，包含邮件相关详细信息", required = true, schema = @Schema(implementation = EmailDto.class))
            @RequestBody EmailDto emailDto) {
        emailService.sendEmail(emailDto);
        return Result.success();
//        return new ResponseEntity<>(emailService.sendEmail(emailDto), HttpStatus.OK);
    }
//
    // 将对应id的邮件状态设置成已读
    @Operation(summary = "将指定ID的邮件设置为已读状态",
            description = "根据传入的邮件ID，将对应的邮件状态设置为已读",
            responses = {
                    @ApiResponse(responseCode = "200", description = "设置已读成功",
                            content = @Content(mediaType = "application/json",
                                    schema = @Schema(implementation = Result.class)))
            })
    @PutMapping("/setRead")
    public Result<?> setRead(
            @Parameter(in = ParameterIn.QUERY, description = "邮件ID", required = true, schema = @Schema(type = "integer"))
            @RequestParam Integer id) {
        emailService.setRead(id);
        return Result.success();
    }



}
